//
//  TBSwitchView.m
//  TBSwitch
//
//  Created by 郑云 on 16/5/9.
//  Copyright © 2016年 ICloud. All rights reserved.
//

#import "TBSwitchView.h"

@interface TBSwitchView()

@property (nonatomic, weak) IBOutlet UIImageView *borderImageView;
@property (nonatomic, weak) IBOutlet UIImageView *thumImageView;

@property (nonatomic, weak) IBOutlet NSLayoutConstraint *thumLeft;
@end
@implementation TBSwitchView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void)awakeFromNib
{
    [super awakeFromNib];
    
    
    
}

#pragma mark --- Touch

-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    
    [self checkPointX:touch];
    
    return YES;
}

//- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
//    
//    [self checkPointX:touch];
//    [self refreshSlider];
//    return YES;
//}

//-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
//    
//    [self checkPointX:touch];
//    
//}


#pragma mark -Function

- (void)checkPointX:(UITouch *)sender
{
    
    if (!self.on) {
        
//        self.thumImageView.transform = CGAffineTransformMakeScale(0.0, 0.0);/
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:0 animations:^{
            
            self.thumImageView.frame = CGRectMake(21, 2, 19, 19);
            [UIView animateWithDuration:0.25 animations:^{
                
                self.thumImageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
                
            }];
            
            [UIView animateWithDuration:0.25 animations:^{
                self.thumImageView.transform = CGAffineTransformMakeScale(0.90, 0.90);

            }];
        } completion:^(BOOL finished) {
            self.on = YES;
            [UIView animateWithDuration:0.15 animations:^{
                self.thumImageView.transform = CGAffineTransformMakeScale(1, 1);
            }];
            
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }];
        
    }
    else
    {
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.1 options:0 animations:^{
            
            self.thumImageView.frame = CGRectMake(2, 2, 19, 19);
            [UIView animateWithDuration:0.25 animations:^{
                
                self.thumImageView.transform = CGAffineTransformMakeScale(0.8, 0.8);
            }];
            
            [UIView animateWithDuration:0.25 animations:^{
                self.thumImageView.transform = CGAffineTransformMakeScale(0.90, 0.90);
                
            }];
        } completion:^(BOOL finished) {
            self.on = NO;
            [UIView animateWithDuration:0.15 animations:^{
                self.thumImageView.transform = CGAffineTransformMakeScale(1, 1);
            }];
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }];
    }
    
}

#pragma mark getter&setter

- (void)setOn:(BOOL)on
{
    _on = on;
    
    [self layoutIfNeeded];
    if (_on) {
        self.thumImageView.image = [UIImage imageNamed:@"ites_btn_on"];
        self.borderImageView.image = [UIImage imageNamed:@"ites_btn_on_bg"];
        //        self.thumImageView.frame = CGRectMake(21, 2, 19, 19);
        self.thumLeft.constant = 21;
    }
    else
    {
        self.thumImageView.image = [UIImage imageNamed:@"ites_btn_off"];
        self.borderImageView.image = [UIImage imageNamed:@"ites_btn_off_bg"];
        //        self.thumImageView.frame = CGRectMake(2, 2, 19, 19);
        self.thumLeft.constant = 2;
    }

}

@end
